<template>
  <div class="venus-list-page">
    <!--签约查询界面-->
    <div class="page-search">
<el-form label-width="160px" style="margin: 20px 50px 20px 0px;" size="mini">
     <el-row type="flex" class="list-items">
       <el-col :span="14">
         <el-form-item label="估值日期:">
          <el-date-picker v-model="table.val_table.start_date" type="date" class="venus-input-middle venus-input-date"
            value-format="yyyyMMdd" placeholder="请输入起始日期" />
          -
          <el-date-picker v-model="table.val_table.end_date" type="date" class="venus-input-middle venus-input-date"
            value-format="yyyyMMdd" placeholder="请输入截止日期" />
        </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="资产名称:">
          <el-select v-model="table.val_table.ftool_code" class="venus-input-middle" filterable clearable placeholder="--请选择--">
            <el-option v-for="item in dict.ftoolCode" :label="item.value" :value="item.text" />
          </el-select>
        </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <div class="venus-btn-box" style="text-align: center;">
          <el-button type="success" @click="search">查询</el-button>
          <el-button type="info" plain @click="resetForm">重置</el-button>
          <el-button type="success" @click="addBean">新增</el-button>
          <el-button type="success" @click="addBean">资产估值</el-button>
        </div>
      </el-row>
      </el-form>
    </div>
    <!--    查询列表返显-->
    <div class="page-list">
      <el-row type="flex" justify="space-between" class="page-list-head">
        <div class="title">查询结果</div>
      </el-row>
      <table-pagination :table-data="table.val_table.tableData" :table-header-config="table.tableHeaderConfig"
        highlight-current-row :total="table.val_table.total" :page-size="table.val_table.limit" :now-page="table.val_table.nowPage"
        :operation-config="table.operationConfig" @current-change="handleCurrentChange" />
    </div>

    <!-- 新增模态框 -->
    <el-dialog :title="form.add.title" :visible.sync="form.add.dialogFormVisible" v-dialogDrag>
      <el-form :model="form.val_form" size="mini" :rules="form.val_form.rules" ref="addForm">
        <el-input type="hidden" v-model="form.val_form.ftool_code" />
        <el-row type="flex">
          <el-col>
            <el-form-item label="估值日期" :label-width="style.formLabelWidth" prop="valdate">
              <el-date-picker v-model="form.val_form.valdate" type="date" value-format="yyyyMMdd" placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="资产类型" :label-width="style.formLabelWidth" prop="t8_sys_adtype_id">
              <el-select v-model="form.val_form.t8_sys_adtype_id" placeholder="请选择" filterable clearable @change="adtype_change">
                <el-option v-for="item in dict.adType" :label="item.value +'-'+item.text" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col>
            <el-form-item label="资产名称" :label-width="style.formLabelWidth" prop="ftool_name">
              <el-select v-model="form.val_form.ftool_name" filterable clearable placeholder="请选择">
                <el-option v-for="item in dict.ftoolName" :label="item.fcd" :value="item.fcd" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="估值净价" :label-width="style.formLabelWidth" prop="valamount">
              <el-input type="number" v-model="form.val_form.valamount" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button type="success" @click="addOk('addForm')">确 定</el-button>
        <el-button type="info" @click="form.add.dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改模态框 -->
    <el-dialog :title="form.edit.title" :visible.sync="form.edit.dialogFormVisible" v-dialogDrag>
      <el-form :model="form.val_form" size="mini" :rules="form.val_form.rules" ref="editForm">
        <el-input type="hidden" v-model="form.val_form.ftool_code" />
        <el-row type="flex">
          <el-col>
            <el-form-item label="估值日期" :label-width="style.formLabelWidth" prop="valdate">
              <el-date-picker v-model="form.val_form.valdate" type="date" value-format="yyyyMMdd" placeholder="选择日期"
                disabled>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="资产类型" :label-width="style.formLabelWidth" prop="t8_sys_adtype_id">
              <el-select v-model="form.val_form.t8_sys_adtype_id" placeholder="请选择" filterable clearable disabled>
                <el-option v-for="item in dict.adType" :label="item.value +'-'+item.text" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col>
            <el-form-item label="资产名称" :label-width="style.formLabelWidth" prop="ftool_name">
              <el-select v-model="form.val_form.ftool_name" filterable clearable placeholder="请选择" disabled>
                <el-option v-for="item in dict.ftoolName" :label="item.fcd" :value="item.fcd" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="估值净价" :label-width="style.formLabelWidth" prop="valamount">
              <el-input type="number" v-model="form.val_form.valamount" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button type="success" @click="editOk('editForm')">确 定</el-button>
        <el-button type="info" @click="form.edit.dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  /* 自定义数据加载 */
  import request from '@/utils/request'
  import * as kk from '@/utils/k-utils'
  import '@/utils/directives'

  export default {
    name: 'assetsVal',
    data() {
      return {
        dict: {
          ftoolCode: [],
          adType: [],
          ftoolName: []
        },
        table: {
          tableHeaderConfig: [ // 表头配置
            {
              prop: 'ad_name',
              label: '资产类型'
            },
            {
              prop: 'ftool_name',
              label: '资产名称'
            },
            {
              prop: 'valdate',
              label: '估值日期'
            },
            {
              prop: 'valamount',
              label: '估值(元)'
            }
          ],
          operationConfig: { // 表格操作配置
            fixed: 'left',
            width: 200,
            operationButton: [
              // 每一个操作。value:操作文字；func:回调函数
              {
                value: '修改',
                func: this.myModify
              },
              {
                value: '删除',
                func: this.myDelete
              }
            ]
          },
          val_table: {
            limit: 5,
            total: 0,
            nowPage: 0,
            tableData: [], // 表格数据
            exeid: 'M8248_search_assets_valuation',
            //查询参数
            ftool_code: '',
            start_date: '',
            end_date: ''
          }
        },
        form: {
          val_form: {
            valdate: '',
            t8_sys_adtype_id: '',
            ftool_name: '',
            ftool_code: '',
            valamount: '',
            rules: {
              valdate: [{
                required: true,
                message: '该项不允许为空',
                trigger: 'blur'
              }],
              t8_sys_adtype_id: [{
                required: true,
                message: '该项不允许为空',
                trigger: 'blur'
              }],
              ftool_name: [{
                required: true,
                message: '该项不允许为空',
                trigger: 'blur'
              }],
              valamount: [{
                required: true,
                message: '该项不允许为空',
                trigger: 'blur'
              }]

            }
          },
          add: {
            title: '新增净值资产估值',
            dialogFormVisible: false
          },
          edit: {
            title: '修改净值资产估值',
            dialogFormVisible: false
          }
        },
        style: {
          formLabelWidth: '130px',
        }
      }
    },
    mounted() {
      // 初始化调用
      this.init()
    },
    methods: {
      init: function() {
          this.table.val_table.tableData=[{ad_name: '债券',ftool_name: '国泰债券',valdate:'20190812',valamount:'6700000'}]
       var params = {}
        kk.k_load_exeid('fund', 'M8248SLT', params).then(rep => {
          if (rep.rows.length > 0) {
            this.dict.ftoolCode = rep.rows
          }
        })

        this.table.val_table.nowPage = 1
        kk.k_load_table(this, 'val_table', 'fund')
      },
      search() {
        this.table.val_table.nowPage = 1
        kk.k_load_table(this, 'val_table', 'fund')
      },
      myModify(item, index) {
        this.copyObj(item, this.form.val_form)
        this.form.edit.dialogFormVisible = true
      },
      myDelete(item, index) {
        this.$confirm('确认删除净值型资产估值吗?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  kk.k_update_exeid('fund', 'M8248_delete_assets_valuation', item).then(rep => {
                    this.resetTable();
                  })
                }).catch(() => {
                  this.$message({
                    type: 'info',
                    message: '已取消删除'
                  })
                })
      },
      resetForm() {
        this.table.val_table.ftool_code = ''
        this.table.val_table.start_date = ''
        this.table.val_table.end_date = ''
      },
      resetTable(){
        //从新加载表格数据
        kk.k_load_table(this, 'val_table', 'fund')
      },
      addBean() {
        //清空数据
        this.copyObj({
          valdate: '',
          t8_sys_adtype_id: '',
          ftool_name: '',
          ftool_code: '',
          valamount: ''
        }, this.form.val_form)
        var params = {}
        kk.k_load_exeid('fund', 'M8000SYSADTYPE', params).then(rep => {
          if (rep.rows.length > 0) {
            this.dict.adType = rep.rows
          }
        })
        this.form.add.dialogFormVisible = true
      },
      handleCurrentChange(val) {
        this.table.val_table.nowPage = val
        kk.k_load_table(this, 'val_table', 'fund')
      },
      /* ===================模态框事件==============*/
      adtype_change() {
        //获取资产类别
        var t8_sys_adtype_id = this.form.val_form.t8_sys_adtype_id
        //交易日期
        var tradedate = this.form.val_form.valdate
        if (t8_sys_adtype_id == "" || tradedate == "") {
          this.$alert('请先输入资产类型与估值日期！', '消息提示', {
            confirmButtonText: '确定',
            callback: action => {}
          })
          return
        }
        var params = {
          t8_sys_adtype_id: t8_sys_adtype_id,
          trade_date: tradedate
        }
        kk.k_load_exeid('fund', 'M825DICT00DJZ', params).then(rep => {
          if (rep.rows.length > 0) {
            this.dict.ftoolName = rep.rows
          }
        })
      },
      copyObj(obj1, obj2) {
        for (var key in obj1) {
          // 原型对象不拷贝
          if (key === '__proto__') {
            continue
          }
          // 不存在的属性不拷贝
          if (!obj2.hasOwnProperty(key)) {
            continue
          }
          // 自动将number转换成string
          if (typeof obj1[key] === 'number') {
            obj1[key] = '' + obj1[key]
          }
          obj2[key] = obj1[key]
        }
      },
      addOk(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var params = this.form.val_form
            params.inputuser = 'admin'; //录入柜员
            var ftcd = params.ftool_name.split("_")
            params.ftool_code = ftcd[0]
            params.ftool_name = ftcd[1]
            params.obj_type = '2' //估值类别：2——资产
            kk.k_update_exeid('fund', 'M8248_save_assets_valuation', params).then(rep => {
              var msg = rep.returnmsg
              if (rep.success) {
                //流程处理
                if (msg.indexOf("流程处理") == -1) {
                  this.resetTable();
                  this.form.add.dialogFormVisible = false
                }
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
      editOk(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var params = this.form.val_form
            params.inputuser = 'admin'; //录入柜员
            kk.k_update_exeid('fund', 'M8248_update_assets_valuation', params).then(rep => {
              var msg = rep.returnmsg
              if (rep.success) {
                //流程处理
                if (msg.indexOf("流程处理") == -1) {
                  this.resetTable();
                  this.form.edit.dialogFormVisible = false
                }
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      }
    }
  }
</script>

<style scoped>
  /*让文本可以被选中*/
  .venus-list-page {
    -moz-user-select: text;
    /*火狐*/
    -webkit-user-select: text;
    /*webkit浏览器*/
    -ms-user-select: text;
    /*IE10*/
    -khtml-user-select: text;
    /*早期浏览器*/
    user-select: text;
  }
</style>
